import { ErrorCode } from '@/config/ErrorCode';
import { UIHelper } from '@/utils/UIHelper';
import axios from 'axios'
import { Logger } from './Logger';


export class HttpUtil {

    public static requestByForm(url: string = "http://212.64.108.20:8888/info", data: object = {}, callback: Function = () => { }) {
        if (url == "") {
            callback && callback(null)
            return;
        }
        Logger.log("HttpUtil.request", url, data)
        axios({
            method: 'post',
            url: url,
            headers: {
                'Content-Type': 'text/plain;charset=UTF-8'
            },
            data: data
        })
            .then(function (response) {
                Logger.log("HttpUtil request", response);
                let respData: object = response["data"]
                callback && callback(respData)
            })
            .catch(function (error) {
                Logger.warn("HttpUtil error", url, data, error);
                callback && callback(error)
            });
    }


    public static request(url: string = "http://212.64.108.20:8888/info", data: object = {}, callback: Function = () => { }) {
        if (url == "") {
            callback && callback(null)
            return;
        }
        Logger.log("HttpUtil.request", url, data)
        axios({
            method: 'post',
            url: url,
            headers: {
                'Content-Type': 'text/plain;charset=UTF-8'
            },
            data: data
        })
            .then(function (response) {
                Logger.log("HttpUtil request", response);
                let respData: object = response["data"]
                let code: number = respData["code"]

                // Logger.log("HttpUtil request===respData=", respData);
                if (code == ErrorCode.Code.Suc) {
                    callback && callback(respData)
                } else {
                    let msg: string = respData["msg"]
                    // Logger.log("HttpUtil request===msg=", msg);
                    UIHelper.showMsg(msg, UIHelper.Msg_Type.error)
                    if (code == ErrorCode.Code.LoginError_token) {
                        UIHelper.logout()
                    }
                }
            })
            .catch(function (error) {
                Logger.warn("HttpUtil error", url, data, error);
                callback && callback(error)
            });
    }


}